package com.anis.project.medcare.command.handler;

import java.util.ArrayList;
import java.util.Date;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;

import com.anis.jpa.medical.model.MedicalReport;
import com.anis.jpa.medical.model.Patient;
import com.anis.project.medcare.Activator;
import com.anis.project.medcare.editors.PatientEditor;
import com.anis.project.medcare.editors.input.PatientEditorInput;

public class SavePatient implements IHandler {

	@Override
	public void addHandlerListener(IHandlerListener handlerListener) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IEditorPart editorPart = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow().getActivePage().getActiveEditor();
		if (editorPart != null && editorPart instanceof PatientEditor) {
			PatientEditor patientEditor = (PatientEditor) editorPart;
			patientEditor.doSave(null);
			patientEditor.setDirty(false);
			
//			PatientEditorInput patientEditorInput = (PatientEditorInput) patientEditor
//					.getEditorInput();
//			Patient patient = patientEditorInput.getPatient();
//
//			ArrayList<MedicalReport> listOfRadioReports = new ArrayList<>();
//			String patientName = patientEditorInput.getPatientName();
//			String lastName = patientEditorInput.getPatientLastName();
//			String patientPhoneString = patientEditorInput.getPatientPhone();
//			Date patientBirthDay = patientEditorInput.getPatientBirthDay();
//			Date creationDate = patientEditorInput.getPatientCreationDate();
//			String rdvDate = patientEditorInput.getPatientNextAppointement();
//			String title = patientEditorInput.getPatientTitle();
//			String patientGender = patientEditorInput.getPatientGender();
//			String patientAddress = patientEditorInput.getPatientAddress();
//			byte[] patientImageByte = patientEditorInput.getPatientImage();
//			Integer patientPhone;
//			try {
//				patientPhone = Integer.valueOf(patientPhoneString);
//			} catch (NumberFormatException e) {
//				patientPhone = 0;
//			}
//
//			if (patient == null) {
//				patient = new Patient(patientName, lastName, patientPhone,
//						patientBirthDay, creationDate, title, patientGender,
//						patientAddress, listOfRadioReports);
//				if (rdvDate != null) {
//					patient.setrdvDate(rdvDate);
//				}
//				patient.setPatientimage(patientImageByte);
//			} else {
//				patient.setPatientaddress(patientAddress);
//				patient.setPatientlastname(lastName);
//				patient.setPatientname(patientName);
//				patient.setPatientsex(patientGender);
//				patient.setPatienttitle(title);
//				patient.setPhone(patientPhone);
//				patient.setPatientBirthday(patientBirthDay);
//				patient.setPatientCreationdate(creationDate);
//				patient.setrdvDate(rdvDate);
//				patient.setPatientimage(patientImageByte);
			}
		//	Activator.getDefault().databaseBinder().savePatient(patient);
			
		

		return null;
	}

	@Override
	public boolean isEnabled() {
		return Activator.getDefault().isConnectiontoDB();
	}

	@Override
	public boolean isHandled() {
		return true;
	}

	@Override
	public void removeHandlerListener(IHandlerListener handlerListener) {
		// TODO Auto-generated method stub

	}

}
